package thread;

public class M1131 {
    public static void main(String[] args){
        NewThread1131 t1=new NewThread1131("One"); //return a NewThread instance
        NewThread1131 t2=new NewThread1131("Two"); //return a NewThread instance
        NewThread1131 t3=new NewThread1131("Three"); //return a NewThread instance

        t1.t.setPriority(Thread.MAX_PRIORITY);
        t2.t.setPriority(Thread.MIN_PRIORITY);
        t3.t.setPriority(Thread.MIN_PRIORITY);

        System.out.println("Thread One is alive: "+t1.t.isAlive());
        System.out.println("Thread Two is alive: "+t2.t.isAlive());
        System.out.println("Thread Three is alive: "+t3.t.isAlive());

        try {
            System.out.println("Waiting for threads to finish.");
            t1.t.join();
            t2.t.join();
            t3.t.join();
        }catch (InterruptedException e){
            System.out.println("Main thread Interrupted");
        }

        System.out.println("Main thread exiting.");
    }
}

class NewThread1131 implements Runnable{
    Thread t;
    private String threadName;

    NewThread1131(String threadName){
        //Create a new ,second thread
        //this，指新线程调用this对象的run（）方法
        //t=new Thread(this,"Demo Thread");
        this.threadName=threadName;
        t=new Thread(this,threadName);
        System.out.println("Child thread "+t);
        t.start();//Start the thread
    }

    //This is the entry point for the second thread

    @Override
    public void run() {
        try {
            for(int i=5;i>0;i--){
                System.out.println("Child Thread "+threadName+": "+i);
                Thread.sleep(500);
            }
        }catch (InterruptedException e){
            System.out.println("Child interrupted.");
        }
        System.out.println("Exiting child thread " +threadName);
    }
}
